package com.ys.everydaynews.utils

import android.app.Activity
import android.app.Dialog
import android.graphics.Color
import android.os.Build
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.core.content.ContextCompat
import com.ys.everydaynews.base.Constants.HOME_BAR

object StatusBarUtils {
    private  var flags:Int=View.SYSTEM_UI_FLAG_LAYOUT_STABLE

    fun setStatusBar(activity: Activity, barType: Int, colorIds: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val mWindow:Window=activity.window
            mWindow.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            mWindow.statusBarColor=Color.TRANSPARENT
            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                flags=View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            }
            activity.window.decorView.systemUiVisibility= flags
            setUiVisibility(mWindow,barType)
            setTextColor(mWindow,ContextCompat.getColor(activity,colorIds))
        }
    }

    private fun setTextColor(mWindow: Window, color: Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWindow.statusBarColor=color
        }
    }

    private fun setUiVisibility(mWindow: Window,statue:Int) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            mWindow.decorView.systemUiVisibility=statue
        }
    }

    fun setTranslucentStatus(dialog:Dialog) {
        val mWindow=dialog.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mWindow!!.decorView.systemUiVisibility=View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            mWindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            mWindow.statusBarColor=Color.TRANSPARENT

        }else{
            mWindow!!.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }
}